﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        ConnectionStringSettings connObj = ConfigurationManager.ConnectionStrings["smsConnectionString"];
        string connectionString = connObj.ConnectionString;
        SqlConnection objConnection = new SqlConnection(connectionString);
                
        //读取UserID
        SqlCommand objCommand = new SqlCommand("Users_FindUserId", objConnection);
        objCommand.CommandType = CommandType.StoredProcedure;

        SqlParameter objParameter = new SqlParameter("@UserName", SqlDbType.NVarChar);
        objCommand.Parameters.Add(objParameter);
        objParameter.Direction = ParameterDirection.Input;
        objParameter.Value = Server.HtmlEncode(((TextBox)(CreateUserWizardSetp.ContentTemplateContainer.FindControl("UserName"))).Text);

        SqlParameter objOutputParameter = new SqlParameter("@UserId", SqlDbType.UniqueIdentifier);
        objCommand.Parameters.Add(objOutputParameter);
        objOutputParameter.Direction = ParameterDirection.Output;

        objConnection.Open();
        objCommand.ExecuteNonQuery();
        Guid m_nUserID = (Guid)objOutputParameter.Value;
        objConnection.Close();

        //写StaffInfo，返回刚刚生成的staffID
        objCommand = new SqlCommand("Users_createStaff", objConnection);
        objCommand.CommandType = CommandType.StoredProcedure;

        objParameter = new SqlParameter("@UserID", SqlDbType.UniqueIdentifier);
        objCommand.Parameters.Add(objParameter);
        objParameter.Direction = ParameterDirection.Input;
        objParameter.Value = m_nUserID;

        objParameter = new SqlParameter("@staffName", SqlDbType.NVarChar);
        objCommand.Parameters.Add(objParameter);
        objParameter.Direction = ParameterDirection.Input;
        objParameter.Value = Server.HtmlEncode(((TextBox)(CreateUserWizardSetp.ContentTemplateContainer.FindControl("UserName"))).Text);

        objParameter = new SqlParameter("@DepartmentID", SqlDbType.Int);
        objCommand.Parameters.Add(objParameter);
        objParameter.Direction = ParameterDirection.Input;
        objParameter.Value = Server.HtmlEncode(((DropDownList)(CreateUserWizardSetp.ContentTemplateContainer.FindControl("DropDepartment"))).SelectedValue);

        objConnection.Open();
        objCommand.ExecuteNonQuery();
        objConnection.Close();

        ProfileCommon profile = this.Profile;
        if (Server.HtmlEncode(((TextBox)(CreateUserWizardSetp.ContentTemplateContainer.FindControl("UserName"))).Text).Length > 0)
            profile = this.Profile.GetProfile(Server.HtmlEncode(((TextBox)(CreateUserWizardSetp.ContentTemplateContainer.FindControl("UserName"))).Text));

        profile.StaffName = Server.HtmlEncode(((TextBox)(CreateUserWizardSetp.ContentTemplateContainer.FindControl("UserName"))).Text);
        profile.DepartmentID = Convert.ToInt32(Server.HtmlEncode(((DropDownList)(CreateUserWizardSetp.ContentTemplateContainer.FindControl("DropDepartment"))).SelectedValue));
        profile.Save();
    }
}
